package teamdamn.view;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.CompoundBorder;
import javax.swing.JLayeredPane;

import java.awt.BorderLayout;

import javax.swing.border.EtchedBorder;
import javax.swing.BoxLayout;

import teamdamn.model.Login;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * This panel is for the Login Functionality
 * @author kjudoy
 */
@SuppressWarnings("serial")
public class LoginPanel extends JPanel {
	
	private JTextField txtUser;
	
	private JPasswordField passUser;
	
	private JRadioButton rdbtnAdmin;
	
	private JRadioButton rdbtnAnalyst;

	private GUI myGUI;
	
	private Login myLogin;
	
	/**
	 * Default Constructor of Analyst Panel
	 * @author kjudoy
	 */
	public LoginPanel(GUI aGUI) { // i tried to pass the main gui on panel
		myGUI = aGUI;
		myLogin = new Login();
		buildLoginPanel();
	}
	
	/**
	 * This method build the GUI of this panel
	 * @author kjudoy
	 */
	private void buildLoginPanel() {		
		
		JPanel panelLogScreen = new JPanel();
		panelLogScreen.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		
		JPanel panelLogType = new JPanel();
		panelLogType.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0)), "Login As:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		rdbtnAdmin = new JRadioButton("Administrator");
		rdbtnAdmin.setVerticalAlignment(SwingConstants.TOP);
		
		rdbtnAnalyst = new JRadioButton("Analyst");
		rdbtnAnalyst.setSelected(true);		
		
		ButtonGroup rdbtnGrpLogType = new ButtonGroup();
		rdbtnGrpLogType.add(rdbtnAnalyst);
		rdbtnGrpLogType.add(rdbtnAdmin);
		
		JButton btnLogin = new JButton("Login");

		JLabel lblUsername = new JLabel("Username");
		
		JLabel lblPassword = new JLabel("Password");
		
		passUser = new JPasswordField();		
		passUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				performLogin();
			}
		});
		
		txtUser = new JTextField();
		txtUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				performLogin();
			}
		});
		txtUser.setColumns(10);
		
		// ACTION LISTENER OF LOGIN
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				performLogin();
			}
		});
		
		// THIS THE GROUP/GRIDBAG LAYOUT FOR LOGIN PANEL		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{50, 200, 50, 0};
		gridBagLayout.rowHeights = new int[]{50, 262, 50, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
			
		GroupLayout gl_panelLogType = new GroupLayout(panelLogType);
		gl_panelLogType.setHorizontalGroup(
			gl_panelLogType.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelLogType.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelLogType.createParallelGroup(Alignment.LEADING)
						.addComponent(rdbtnAdmin)
						.addComponent(rdbtnAnalyst))
					.addContainerGap(9, Short.MAX_VALUE))
		);
		gl_panelLogType.setVerticalGroup(
			gl_panelLogType.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelLogType.createSequentialGroup()
					.addComponent(rdbtnAdmin)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(rdbtnAnalyst)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panelLogType.setLayout(gl_panelLogType);
		
		GroupLayout gl_panelLogScreen = new GroupLayout(panelLogScreen);
		gl_panelLogScreen.setHorizontalGroup(
			gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelLogScreen.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelLogScreen.createSequentialGroup()
							.addComponent(panelLogType, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(btnLogin, GroupLayout.DEFAULT_SIZE, 140, Short.MAX_VALUE)
							.addContainerGap())
						.addGroup(Alignment.TRAILING, gl_panelLogScreen.createSequentialGroup()
							.addGroup(gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panelLogScreen.createSequentialGroup()
									.addComponent(lblUsername)
									.addGap(10)
									.addComponent(txtUser, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panelLogScreen.createSequentialGroup()
									.addGap(2)
									.addComponent(lblPassword)
									.addGap(10)
									.addComponent(passUser, GroupLayout.PREFERRED_SIZE, 205, GroupLayout.PREFERRED_SIZE)))
							.addGap(23))))
		);
		gl_panelLogScreen.setVerticalGroup(
			gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelLogScreen.createSequentialGroup()
					.addContainerGap(68, Short.MAX_VALUE)
					.addGroup(gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelLogScreen.createSequentialGroup()
							.addGap(3)
							.addComponent(lblUsername))
						.addComponent(txtUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panelLogScreen.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelLogScreen.createSequentialGroup()
							.addGap(3)
							.addComponent(lblPassword))
						.addComponent(passUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panelLogScreen.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnLogin, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelLogType, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		panelLogScreen.setLayout(gl_panelLogScreen);
		
		GridBagConstraints gbc_panelLogScreen = new GridBagConstraints();
		gbc_panelLogScreen.insets = new Insets(0, 0, 5, 5);
		gbc_panelLogScreen.gridx = 1;
		gbc_panelLogScreen.gridy = 1;
		add(panelLogScreen, gbc_panelLogScreen);		
	}
	
	/**
	 * This method checks if the user input for login matches the records.
	 * It opens an OptionPane when the input is incorrect.
	 * @author Joel Hunsaker
	 * @author jrsto674
	 * @author kjudoy
	 */
	private final void performLogin() {
		String username = txtUser.getText();
		char[] password = passUser.getPassword();
		String role;
		
		if(rdbtnAnalyst.isSelected()) {
			role = "Analyst";
		} else {
			role = "Admin";
		}
		
		if(myLogin.verifyLogin(username, password, role)) {
			myGUI.updateUsername(username);
			if(role.equals("Analyst")){
				myGUI.loginToAnalystPanel();
			} else {
				myGUI.loginToRefresh();
			}
		} else {
			JFrame frame = new JFrame("MSG Login Fail");
			JOptionPane.showMessageDialog(frame,
					"Incorrect username or password.\n\nNote: Always check for login type.",
			        "Login Failure",
			        JOptionPane.INFORMATION_MESSAGE);
			
		}
	}
	
	/**
	 * Resets username and password field to empty, set Analyst radio
	 * button to selected.
	 * @author jrsto674
	 */
	public void resetFields() {
		txtUser.setText("");
		passUser.setText("");
		rdbtnAnalyst.setSelected(true);
	}
}
